/*
 * metadata.h
 *      -- define public structure shared by all encoded components
 *
 * Author: Qiu Shuang
 * Date: Mar 1st, 2011
 */
#ifndef METADATA_H
#define METADATA_H

#define DEBUG

#define SYNCH_CODE 0xfff
#define ONE_BIT    0x1
#define TWO_BITS   0x3
#define THREE_BITS 0x7
#define FOUR_BITS  0xf

/* MPEG frame structure parameter */
#define GRANULE                     2
#define CHANNEL                     2
#define SCALEFACTOR_CLASS           4
#define MIXEDLONGWIN_COUNTS         2
#define PURELONGWIN_COUNTS          3
#define SUBBLOCK_COUNTS             3
#define SHORTWIN_PER_GRANULE        3

#define MIXEDLONGWIN_SCALEFACT      8
#define MIXEDSHORTWIN_SCALEFACT     9

#define SIDEINFO_SIZE               32
#define CRITICAL_BANDS              22
#define DIVIDED_BANDS               9
#define LONGWIN_SCALEFACTABLE       23
#define SHORTWIN_SCALEFACTABLE      14

#define BITRATE_INX_COUNTS          16
#define SAMPLE_INX_COUNTS           4

#define MAX_BUFSIZE                 1728
#define MAX_INTERMEDIATE_BUFSIZE    1920

#define HUFFMANTABLE_COUNTS         34

/* MPEG1 layerIII subband related information */
#define SUBBAND_COUNTS      32
#define SUBBAND_SAMPLES     18
#define GRANULE_SAMPLES     576     /* 32*18 */

#define ALIGNED_SAMPLES     192     /* one-third of the total samples */

/* mathmetical constant */
#define PI                  3.1415926
#define SQRT_2              1.41421356

#define OK    0         /* operation completes successfully */
#define ERROR -1        /* error occurs when processing */

/* MPEG version information */
typedef enum {MPEG2 = 0, MPEG1} MPEGVersion;

/* MPEG layer information */
typedef enum {NOLAYER = 0, LAYER1, LAYER2, LAYER3} MPEGLayer;

/* MPEG mode information */
typedef enum {STEREO = 0, JSTEREO, DUALCH, SINGLECH, MULTICH} MPEGMode;

/* MPEG encoding bitrate information */
const int bitrates[ 2 ][ 3 ][ 16 ] = {
    {{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
     {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
     {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}},

    {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}}
};

/* MPEG sampling freqency table */
const double samplingrate[ 2 ][ 4 ] = {{44.1, 48, 32, 0}, {22.05, 24, 16, 0}};

/* macro swap high byte and low byte to ease the wrong order case */
#define HIGH_TO_LOW(byte)   ((byte << 8) | (byte >> 8))

/* macro get minmum and maximun from two */
#define MIN(a, b)           ((a)<=(b)?(a):(b))
#define MAX(a, b)           ((a)>=(b)?(a):(b))

typedef long int ChannelDataLong[SUBBAND_COUNTS][SUBBAND_SAMPLES];
typedef double ChannelDataDouble[SUBBAND_COUNTS][SUBBAND_SAMPLES];

#endif